Phpdoc comments and place holder. Part of the subpackage "maintenance", archives...
[lhc/web/wiklou.git] / maintenance / convertLinks.inc
1 <?php
2 /**
3 * @todo document
4 * @package MediaWiki
5 * @subpackage Maintenance
6 */
7
8 /** */
9 function convertLinks() {
10 print "Converting links table to ID-ID...\n";
11
12 global $wgLang, $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname;
13 global $noKeys, $logPerformance, $fh;
14
15 $numRows = $tuplesAdded = $numBadLinks = $curRowsRead = 0; #counters etc
16 $totalTuplesInserted = 0; # total tuples INSERTed into links_temp
17
18 $reportCurReadProgress = true; #whether or not to give progress reports while reading IDs from cur table
19 $curReadReportInterval = 1000; #number of rows between progress reports
20
21 $reportLinksConvProgress = true; #whether or not to give progress reports during conversion
22 $linksConvInsertInterval = 1000; #number of rows per INSERT
23
24 $initialRowOffset = 0;
25 #$finalRowOffset = 0; # not used yet; highest row number from links table to process
26
27 # Overwrite the old links table with the new one. If this is set to false,
28 # the new table will be left at links_temp.
29 $overwriteLinksTable = true;
30
31 # Don't create keys, and so allow duplicates in the new links table.
32 # This gives a huge speed improvement for very large links tables which are MyISAM. (What about InnoDB?)
33 $noKeys = false;
34
35
36 $logPerformance = false; # output performance data to a file
37 $perfLogFilename = "convLinksPerf.txt";
38 #--------------------------------------------------------------------
39
40 $dbw =& wfGetDB( DB_MASTER );
41 extract( $dbw->tableNames( 'cur', 'links', 'links_temp', 'links_backup' ) );
42
43 $res = $dbw->query( "SELECT l_from FROM $links LIMIT 1" );
44 if ( $dbw->fieldType( $res, 0 ) == "int" ) {
45 print "Schema already converted\n";
46 return;
47 }
48
49 $res = $dbw->query( "SELECT COUNT(*) AS count FROM $links" );
50 $row = $dbw->fetchObject($res);
51 $numRows = $row->count;
52 $dbw->freeResult( $res );
53
54 if ( $numRows == 0 ) {
55 print "Updating schema (no rows to convert)...\n";
56 createTempTable();
57 } else {
58 if ( $logPerformance ) { $fh = fopen ( $perfLogFilename, "w" ); }
59 $baseTime = $startTime = getMicroTime();
60 # Create a title -> cur_id map
61 print "Loading IDs from $cur table...\n";
62 performanceLog ( "Reading $numRows rows from cur table...\n" );
63 performanceLog ( "rows read vs seconds elapsed:\n" );
64
65 $dbw->setBufferResults( false );
66 $res = $dbw->query( "SELECT cur_namespace,cur_title,cur_id FROM $cur" );
67 $ids = array();
68
69 while ( $row = $dbw->fetchObject( $res ) ) {
70 $title = $row->cur_title;
71 if ( $row->cur_namespace ) {
72 $title = $wgLang->getNsText( $row->cur_namespace ) . ":$title";
73 }
74 $ids[$title] = $row->cur_id;
75 $curRowsRead++;
76 if ($reportCurReadProgress) {
77 if (($curRowsRead % $curReadReportInterval) == 0) {
78 performanceLog( $curRowsRead . " " . (getMicroTime() - $baseTime) . "\n" );
79 print "\t$curRowsRead rows of $cur table read.\n";
80 }
81 }
82 }
83 $dbw->freeResult( $res );
84 $dbw->setBufferResults( true );
85 print "Finished loading IDs.\n\n";
86 performanceLog( "Took " . (getMicroTime() - $baseTime) . " seconds to load IDs.\n\n" );
87 #--------------------------------------------------------------------
88
89 # Now, step through the links table (in chunks of $linksConvInsertInterval rows),
90 # convert, and write to the new table.
91 createTempTable();
92 performanceLog( "Resetting timer.\n\n" );
93 $baseTime = getMicroTime();
94 print "Processing $numRows rows from $links table...\n";
95 performanceLog( "Processing $numRows rows from $links table...\n" );
96 performanceLog( "rows inserted vs seconds elapsed:\n" );
97
98 for ($rowOffset = $initialRowOffset; $rowOffset < $numRows; $rowOffset += $linksConvInsertInterval) {
99 $sqlRead = "SELECT * FROM $links ".wfLimitResult($linksConvInsertInterval,$rowOffset);
100 $res = $dbw->query($sqlRead);
101 if ( $noKeys ) {
102 $sqlWrite = array("INSERT INTO $links_temp (l_from,l_to) VALUES ");
103 } else {
104 $sqlWrite = array("INSERT IGNORE INTO $links_temp (l_from,l_to) VALUES ");
105 }
106
107 $tuplesAdded = 0; # no tuples added to INSERT yet
108 while ( $row = $dbw->fetchObject($res) ) {
109 $fromTitle = $row->l_from;
110 if ( array_key_exists( $fromTitle, $ids ) ) { # valid title
111 $from = $ids[$fromTitle];
112 $to = $row->l_to;
113 if ( $tuplesAdded != 0 ) {
114 $sqlWrite[] = ",";
115 }
116 $sqlWrite[] = "($from,$to)";
117 $tuplesAdded++;
118 } else { # invalid title
119 $numBadLinks++;
120 }
121 }
122 $dbw->freeResult($res);
123 #print "rowOffset: $rowOffset\ttuplesAdded: $tuplesAdded\tnumBadLinks: $numBadLinks\n";
124 if ( $tuplesAdded != 0 ) {
125 if ($reportLinksConvProgress) {
126 print "Inserting $tuplesAdded tuples into $links_temp...";
127 }
128 $dbw->query( implode("",$sqlWrite) );
129 $totalTuplesInserted += $tuplesAdded;
130 if ($reportLinksConvProgress)
131 print " done. Total $totalTuplesInserted tuples inserted.\n";
132 performanceLog( $totalTuplesInserted . " " . (getMicroTime() - $baseTime) . "\n" );
133 }
134 }
135 print "$totalTuplesInserted valid titles and $numBadLinks invalid titles were processed.\n\n";
136 performanceLog( "$totalTuplesInserted valid titles and $numBadLinks invalid titles were processed.\n" );
137 performanceLog( "Total execution time: " . (getMicroTime() - $startTime) . " seconds.\n" );
138 if ( $logPerformance ) { fclose ( $fh ); }
139 }
140 #--------------------------------------------------------------------
141
142 if ( $overwriteLinksTable ) {
143 $dbConn = Database::newFromParams( $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname );
144 if (!($dbConn->isOpen())) {
145 print "Opening connection to database failed.\n";
146 return;
147 }
148 # Check for existing links_backup, and delete it if it exists.
149 print "Dropping backup links table if it exists...";
150 $dbConn->query( "DROP TABLE IF EXISTS $links_backup", DB_MASTER);
151 print " done.\n";
152
153 # Swap in the new table, and move old links table to links_backup
154 print "Swapping tables '$links' to '$links_backup'; '$links_temp' to '$links'...";
155 $dbConn->query( "RENAME TABLE links TO $links_backup, $links_temp TO $links", DB_MASTER );
156 print " done.\n\n";
157
158 $dbConn->close();
159 print "Conversion complete. The old table remains at $links_backup;\n";
160 print "delete at your leisure.\n";
161 } else {
162 print "Conversion complete. The converted table is at $links_temp;\n";
163 print "the original links table is unchanged.\n";
164 }
165 }
166
167 #--------------------------------------------------------------------
168
169 function createTempTable() {
170 global $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname;
171 global $noKeys;
172 $dbConn = Database::newFromParams( $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname );
173
174 if (!($dbConn->isOpen())) {
175 print "Opening connection to database failed.\n";
176 return;
177 }
178 $links_temp = $dbConn->tableName( 'links_temp' );
179
180 print "Dropping temporary links table if it exists...";
181 $dbConn->query( "DROP TABLE IF EXISTS $links_temp");
182 print " done.\n";
183
184 print "Creating temporary links table...";
185 if ( $noKeys ) {
186 $dbConn->query( "CREATE TABLE $links_temp ( " .
187 "l_from int(8) unsigned NOT NULL default '0', " .
188 "l_to int(8) unsigned NOT NULL default '0')");
189 } else {
190 $dbConn->query( "CREATE TABLE $links_temp ( " .
191 "l_from int(8) unsigned NOT NULL default '0', " .
192 "l_to int(8) unsigned NOT NULL default '0', " .
193 "UNIQUE KEY l_from(l_from,l_to), " .
194 "KEY (l_to))");
195 }
196 print " done.\n\n";
197 }
198
199 function performanceLog( $text ) {
200 global $logPerformance, $fh;
201 if ( $logPerformance ) {
202 fwrite( $fh, $text );
203 }
204 }
205
206 function getMicroTime() { # return time in seconds, with microsecond accuracy
207 list($usec, $sec) = explode(" ", microtime());
208 return ((float)$usec + (float)$sec);
209 }
210
211
212
213 ?>